import { useState } from "react";
import { useUserToken } from "@/store/userStore";
import { wechatLogin, type WechatLoginReq } from "@/api/services/auth";

interface WechatLoginParams {
	code: string;
	state?: string;
}

export function useWechatLogin() {
	const [isLoading, setIsLoading] = useState(false);
	const { setToken, setUserInfo } = useUserToken();

	const login = async (params: WechatLoginReq) => {
		try {
			setIsLoading(true);
			const response = await wechatLogin(params);

			// 保存 token 和用户信息
			setToken(response.token);
			setUserInfo(response.userInfo);

			return response;
		} finally {
			setIsLoading(false);
		}
	};

	return {
		login,
		isLoading,
	};
}
